///*
//
// 
//*/
//
//#include <stdio.h>
//#include <stdlib.h>
//#include <math.h>
//#include <string.h>
//#include "Separator.h"
/// *
//void Separator::processFFTFrame(float * sample, int size, float  freq){
//	float *dataReal = new float[blockSize];
//	memset(dataReal, 0, blockSize * sizeof(float)); // Zero padding
//	memcpy(dataReal, sample, size * sizeof(float));
//	processWin(dataReal);
//	float *dataImg = new float[blockSize];
//	memset(dataImg, 0, blockSize * sizeof(float));
//	
//	fft(dataReal, dataImg, blockSize, tempReal, tempImg);
//	//print(dataReal);
//	//print(dataImg);
//	dataImg[0] = 0.0;
//	dataImg[blockSize -1] = 0.0;
//	memcpy(fftReal, dataReal, blockSize * sizeof(float));
//	memcpy(fftImg, dataImg, blockSize * sizeof(float));
//	delete[]dataReal;
//	delete[]dataImg;
//}
//
//void Separator::processIFFTFrame(){
//	//Ifft Noise
//	ifft(noiseReal, noiseImg, blockSize, tempReal, tempImg);
//	memcpy(noiseFrame, noiseReal, blockSize * sizeof(float));
//	//processDeWin(noiseFrame);
//	//processWin(harmonicFrame);
//	//print(noiseFrame);
//	// Ifft Harmonic
	//ifft(harmonicReal, harmonicImg, blockSize, tempReal, tempImg);
	//memcpy(harmonicFrame, harmonicReal, blockSize * sizeof(float));
//	//processWin(harmonicFrame);
//	processDeWin(harmonicFrame);
//	}
//
//void Separator::separatorFrame(float freq){
//	
//	memcpy(noiseReal, fftReal, blockSize * sizeof(float));
//	memcpy(noiseImg, fftImg, blockSize * sizeof(float));
//	
//	//memset(harmonicReal, 0, blockSize * sizeof(float));
//	//memset(harmonicImg, 0, blockSize * sizeof(float));
//	memcpy(harmonicReal, fftReal, blockSize * sizeof(float));
//	memcpy(harmonicImg, fftImg, blockSize * sizeof(float));
//
//	frequencyHarmonic = new float[(int)(maxFreq / freq) + 1];
//	
//	count = 1;
//	
//	/*for (int bin = 0; bin < blockSize; bin++)
//	{*/
//		int i = 1;
//		while (freq*i < maxFreq){
//			frequencyHarmonic[count] = freq*i;
//			int binstart = ftob(freq*i) - binPadding / 2;
//			int binend = ftob(freq*i) + binPadding / 2;
//			//if (binstart <= bin  && bin <= binend)
//			//printf("bin Start End %d %d \n", binstart, binend);
//			noiseImg[0] = 0.0;
//			noiseImg[blockSize-1] = 0.0;
//			for (int bin = binstart; bin <= binend; bin++)
//			{
//				//if CURRENT_BIN is within bin padding for freq and freq is not above maxFreq, send to harmonics channel
//				harmonicReal[bin] = fftReal[bin];
//				harmonicImg[bin] = fftImg[bin];
//
//				noiseReal[ bin] = 0.0;//fftReal[bin];
//				noiseImg[ bin] = 0.0;// / fftImg[bin];
//
//				noiseReal[blockSize  -1 - bin] = 0.0;//fftReal[bin];
//				noiseImg[blockSize - 1 - bin] = 0.0;// / fftImg[bin];
//			}
//			//else
//			//{
//			//	//otherwise, send to noise channel
//			//	noiseReal[bin] = fftReal[bin];
//			//	noiseImg[bin] = fftImg[bin];
//			//}
//			i++;
//			count = i;
//		}
//		
//	//}
//	
//}
//
//
//
//void Separator::ResynthesisHamolet(float freq){
//	//for (int j = 0; j < blockSize; j++){
//	//	resynthesis[j] = 0;
//	//	for (int i = 1; i < count; i++){
//	//		int bin = ftob(freq * i);
//	//		if (bin < blockSize){
//	//			
//	//			phase[i] += ((1.f / samplerate) * freq * i) * 2.0*PI;
//	//			resynthesis[j] += sqrt(fftImg[bin] * fftImg[bin] + fftReal[bin] * fftReal[bin])/(float)blockSize *sinf(phase[i]);
//	//			//printf("i = %d  freq =  %5.5f  Bin = %d phase[%d]  %5.5f  sin phase =  %5.5f ,  amplitude %5.5f  %f\n",
//	//			//	i, freq*i, bin, bin, phase[i], sinf(phase[i]), sqrt(fftImg[bin] * fftImg[bin] + fftReal[bin] * fftReal[bin]), resynthesis[j]);
//	//		}
//	//		
//	//	}
//	//}
//	
//	float *temp = new float [(count) * blockSize];
//	memset(temp, 0, (count) * blockSize * sizeof(float));
//
//	for (int i = 1; i <= count; i++){// Each Harmonic
//
//
//		phase[i - 1] = 0;
//		float * ahamonic = &temp[(i-1) *blockSize];
//		for (int j = 0; j < blockSize; j++){ // Get Sinwave At a frequency store ahamonic
//			phase[i -1] += ((1.f / samplerate) * freq * (i)) * 2.0*PI;
//			ahamonic[j] += sinf(phase[i -1]);
//			//printf("i = %d  freq =  %5.5f  Bin = %d phase[%d]  %5.5f  sin phase =  %5.5f ,  amplitude %5.5f  %f\n",
//			//	i, freq*i, bin, bin, phase[i], sinf(phase[i]), sqrt(fftImg[bin] * fftImg[bin] + fftReal[bin] * fftReal[bin]), resynthesis[j]);
//		}
//		// Get harmonic sinwave
//		int bin = ftob(freq * (i+1));
//		if (bin < blockSize){
//			memset(harmonicReal,0,blockSize * sizeof(float));
//			memset(harmonicImg, 0, blockSize * sizeof(float));
//			memcpy(&harmonicReal[bin - binPadding / 2], &fftReal[bin - binPadding / 2], (1 + (binPadding / 2) * 2)* sizeof(float));
//			memcpy(&harmonicImg[bin - binPadding / 2], &fftImg[bin - binPadding / 2], (1 + (binPadding / 2) * 2)* sizeof(float));
//
//			memcpy(&harmonicReal[blockSize - (bin + binPadding / 2)], &fftReal[blockSize - (bin + binPadding / 2)], (1 + (binPadding / 2) * 2)* sizeof(float));
//			memcpy(&harmonicImg[blockSize - (bin + binPadding / 2)], &fftImg[blockSize - (bin + binPadding / 2)], (1 + (binPadding / 2) * 2)* sizeof(float));
//
//			ifft(harmonicReal, harmonicImg, blockSize, tempReal, tempImg);
//			memcpy(harmonicFrame, harmonicReal, blockSize * sizeof(float));
//			normalizeAmplitude(harmonicFrame);
//		}
//		// get amplitude
//		for (int k = 0; k < blockSize; k++){
//			ahamonic[k] *= harmonicFrame[k];
//		}
//	}
//
//	// Resynthesis each harmonic
//	for (int j = 0; j < blockSize; j++){
//		resynthesis[j] = 0;
//		for (int i = 1; i <= count ; i++){
//			float * ahamonic = &temp[(i-1) *blockSize];
//			resynthesis[j] += ahamonic[j];
//		}
//	}
//	delete temp;
//}
//
//void Separator::SumOverlap(float *first, float *second, int overlap){
//
//	for (int i = 0; i < overlap; i++){
//		first[i] += second[i];
//		first[i] /= 2.0;
//	}
//}
//
//void Separator::processSpectrum(float *sample, int size_){
//	for (int i = 0; i < size; i++){
//		//sample[i] = sqrtf(noiseReal[i] * noiseReal[i] + noiseImg[i] * noiseImg[i]);
//		sample[i] = sqrtf(fftReal[i] * fftReal[i] + fftImg[i] * fftImg[i]);
//	}
//}
//
//
//void Separator::processBlock(float * sample, int size){
//	/*this->size = size;
//	if (time)
//		sample = (sample - overlap / 2);*/
//	
//	// Get Frequency
//	helmholtz->iosamples(sample, hhout, size);
//	freq = initFreq;
//	float harmonic;
//	//Helmoholtz
//	if (helmholtz->getperiod() > 0.f){
//		freq = float(samplerate) / helmholtz->getperiod();
//
//		// compensate for harmonic jumping //BYPASS THIS IF NO FREQUENCY SPECIFIED
//		if (initFreq > 0){
//			harmonic = freq / initFreq;
//			if (harmonic < 1)
//				freq = freq * round(1 / harmonic);
//			else
//				freq = freq / round(harmonic);
//
//			if (helmholtz->getfidelity() >= HelFidelity)
//				hold_freq = freq;
//			else
//				freq = hold_freq;
//		}
//		else{
//			hold_freq = freq;
//		}
//	}
//
//	// Process Separator
//	
//	
//	count = (int)(maxFreq / freq)  + 1;
//	printf(" %f %d \n", freq, count);
//	processFFTFrame(sample, size, freq);
//	//separatorFrame(freq);
//	//processIFFTFrame(); // noiseFrame
//	//normalizeAmplitude();//
//	ResynthesisHamolet(freq); //resynthesis
//
//}
//
//void Separator::processData(int length, float * rawChannelInput, float *noiseChannel, float * hamonicChannel){
//	int ll = 1;
//
//	for (int blockStart = 0; blockStart < length; blockStart += (blockSize -  overlap))
//	{
//		int numSamplesInBlock = blockSize;
//
//		if (blockStart + blockSize > length)
//			numSamplesInBlock = length % blockSize;
//		int lap = 0;
//		if (blockStart != blockSize){
//			lap = overlap;
//		}
//		if (numSamplesInBlock)
//		{
//			processBlock(rawChannelInput + blockStart, numSamplesInBlock);
//			printf(" %d  %d \n", blockStart, numSamplesInBlock);
//			if (noiseChannel && numSamplesInBlock == blockSize && ll < 2){
//				memcpy(noiseChannel + blockStart + lap, this->getNoise() + lap, (numSamplesInBlock - lap)*sizeof(float));
//				//SumOverlap(noiseChannel + blockStart, this->getNoise(), lap);
//				//printf(" %d \n", blockStart);
//			}
//
//			//print(noiseChannel);
//			if (hamonicChannel && numSamplesInBlock == blockSize && ll < 2){
//				memcpy(hamonicChannel + blockStart + lap, this->getHarmonic() + lap, (numSamplesInBlock - lap)*sizeof(float));
//			}
//			ll++;
//		}
//
//	}
//	//for (int blockStart = blockSize; blockStart < length;  ){
//	//	int numSamplesInBlock = blockSize ;
//
//	//	if (blockStart + blockSize - overlap > length)
//	//		numSamplesInBlock = length % blockSize;
//
//	//	if (numSamplesInBlock)
//	//	{
//	//		int index_move = 0;
//	//		int index_rawdata = 0;
//	//		if (blockStart != blockSize){
//	//			if (numSamplesInBlock != blockSize)
//	//				index_move = 0.0 ;
//	//			else
//	//				index_move = overlap;
//	//			index_rawdata = blockStart - overlap / 2;
//	//		}
//	//		//processBlock(/*inputFile->GetSafePt(analysisChannel, true)*/rawChannelInput + blockStart, numSamplesInBlock, inputFile);
//	//		processBlock(rawChannelInput + blockStart, numSamplesInBlock);
//	//		printf(" %d  %d \n", blockStart, numSamplesInBlock);
//	//		if (noiseChannel && numSamplesInBlock == blockSize /*&& ll < 4*/){
//	//			memcpy(noiseChannel + index_rawdata, this->getNoise(), (numSamplesInBlock - index_move)*sizeof(float));
//	//			//printf(" %d \n", blockStart);
//	//		}
//
//	//		//print(noiseChannel);
//	//		if (hamonicChannel && numSamplesInBlock == blockSize /*&& ll < 4*/){
//	//			memcpy(hamonicChannel + index_rawdata, this->getHarmonic(), (numSamplesInBlock - index_move) *sizeof(float));
//	//		}
//	//		// Test Channel
//	//		//if (configPre->TestChannel)
//	//		//{
//	//		//	//if (1 && ll > 1 && ll <3)
//	//		//	//separator->processSpectrum(testChannel + blockStart, numSamplesInBlock);
//	//		//	//else{
//	//		//	//	memset(testChannel + blockStart, -1, numSamplesInBlock*sizeof(float));
//	//		//	//}
//	//		//}
//	//		ll++;
//	//	}
//	//	time++;
//
//	//	blockStart += blockSize - overlap;
//	//}
//}
